/**
 * 2013 2013-5-13
 * @author asdlkghdksjg
 */
package com.surfilter.dp.view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.apache.tools.ant.Project;

import com.surfilter.code.gen.task.JavaCodeGenTask;
import com.surfilter.code.gen.task.JsCodeGenTask;
import com.surfilter.dp.constant.Constants;
import com.surfilter.dp.context.ContextHolder;
import com.surfilter.dp.model.ModuleModel;
import com.surfilter.dp.util.PropertyFileManager;

/**
 * @author asdlkghdksjg
 * 
 */
public class CodeGenPanel extends JPanel {
	
	JButton btn_genJava;
	JButton btn_genJS;
	JCheckBox checkBox_override;
	private JRadioButton radio_mysql;
	private JRadioButton radio_oracle;
	private JLabel lbloraclesql;
	
	/**
	 * Create the panel.
	 */
	public CodeGenPanel() {
		initView();
		initListener();
	}

	/**
	 * 
	 */
	private void initListener() {
		final ContextHolder context = ContextHolder.getInstance();
		btn_genJava.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ModuleModel module = context.getModuleTree().getSelectedModule();
				if(module != null){
					if(module.getModuleType() == Constants.TYPE_FUN){
						boolean override = checkBox_override.isSelected();
						Project project = new Project();
						JavaCodeGenTask task = new JavaCodeGenTask();
						task.setDataSource(Constants.CODE_DATASOURCE);
						task.setPkg(Constants.CODE_PKG);
						task.setJavaModPath(PropertyFileManager.getProp("project_path") + File.separator+  "src");
						task.setIsCover(override ? "true":"false");
						task.setDialet(radio_mysql.isSelected()?"mysql":"oracle");
						task.setPattern(module.getModuleName());
						task.setProject(project);
						task.execute();
						if(ContextHolder.isGenJavaSuccess){
							JOptionPane.showMessageDialog(context.getMainWin(), "JAVA代码生成成功");
						}else{
							ContextHolder.isGenJavaSuccess = false;
						}
					}else{
						JOptionPane.showMessageDialog(context.getMainWin(), "只能为功能模块生成代码");
					}
				}else{
					JOptionPane.showMessageDialog(context.getMainWin(), "请在左侧模块树选择一功能模块");
				}
			}
		});
		btn_genJS.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ModuleModel module = context.getModuleTree().getSelectedModule();
				if(module != null){
					if(module.getModuleType() == Constants.TYPE_FUN){
						boolean override = checkBox_override.isSelected();
						Project project = new Project();
						JsCodeGenTask task = new JsCodeGenTask();
						task.setDataSource(Constants.CODE_DATASOURCE);
						task.setJsPrefix(Constants.CODE_JSPREFIX);
						task.setJsModPath(PropertyFileManager.getProp("project_path") + Constants.CODE_JSMODPATH);				
						task.setIsCover(override ? "true":"false");
						task.setPattern(module.getModuleName());
						task.setProject(project);
						task.execute();
						if(ContextHolder.isGenJSSuccess){
							JOptionPane.showMessageDialog(context.getMainWin(), "JS代码生成成功");
						}else{
							ContextHolder.isGenJSSuccess = false;
						}
					}else{
						JOptionPane.showMessageDialog(context.getMainWin(), "只能为功能模块生成代码");
					}
				}else{
					JOptionPane.showMessageDialog(context.getMainWin(), "请在左侧模块树选择一功能模块");
				}
			}
		});

	}
	/**
	 * 
	 */
	private void initView() {
		setLayout(null);
		checkBox_override = new JCheckBox("是否覆盖原有代码");
		checkBox_override.setBackground(Color.WHITE);
		checkBox_override.setSelected(true);
		checkBox_override.setBounds(230, 68, 183, 30);
		add(checkBox_override);
		
		btn_genJava = new JButton("生成Java代码");
		btn_genJava.setBounds(109, 208, 115, 30);
		add(btn_genJava);
		btn_genJS = new JButton("生成JS代码");
		btn_genJS.setBounds(375, 208, 115, 30);
		add(btn_genJS);
		
		JLabel lblSql = new JLabel("SQL方言：");
		lblSql.setBounds(109, 127, 54, 15);
		add(lblSql);
		
		
		radio_mysql = new JRadioButton("MySQL");
		radio_mysql.setBackground(Color.WHITE);
		radio_mysql.setBounds(188, 123, 121, 23);
		add(radio_mysql);
		radio_mysql.setSelected(true);
		
		radio_oracle = new JRadioButton("Oracle");
		radio_oracle.setBackground(Color.WHITE);
		radio_oracle.setBounds(375, 123, 121, 23);
		add(radio_oracle);
		
		ButtonGroup bg = new ButtonGroup();
		bg.add(radio_mysql);
		bg.add(radio_oracle);
		
		lbloraclesql = new JLabel("注 : 生成Oracle方言SQL语句时需找到对应的SQL.xml文件将其中的!SEQUENCE改为表对应的序列名");
		lbloraclesql.setBounds(24, 269, 552, 30);
		add(lbloraclesql);
		
	}
}
